package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.DbTrash;
import com.ruoyi.system.mapper.DbTrashMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DbLaunchMapper;
import com.ruoyi.system.domain.DbLaunch;
import com.ruoyi.system.service.IDbLaunchService;

/**
 * 投放收录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-27
 */
@Service
public class DbLaunchServiceImpl implements IDbLaunchService 
{
    @Autowired
    private DbLaunchMapper dbLaunchMapper;
    @Autowired
    private DbTrashMapper dbTrashMapper;

    /**
     * 查询投放收录
     * 
     * @param id 投放收录主键
     * @return 投放收录
     */
    @Override
    public DbLaunch selectDbLaunchById(Long id)
    {
        return dbLaunchMapper.selectDbLaunchById(id);
    }

    /**
     * 查询投放收录列表
     * 
     * @param dbLaunch 投放收录
     * @return 投放收录
     */
    @Override
    public List<DbLaunch> selectDbLaunchList(DbLaunch dbLaunch)
    {
        return dbLaunchMapper.selectDbLaunchList(dbLaunch);
    }

    /**
     * 新增投放收录
     * 
     * @param dbLaunch 投放收录
     * @return 结果
     */
    @Override
    public int insertDbLaunch(DbLaunch dbLaunch)
    {
        //设置用户id
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        dbLaunch.setCreateTime(DateUtils.getNowDate());
        //设置投放时间
        dbLaunch.setLaunchDate(DateUtils.getDate());
        dbLaunch.setLaunchTime(DateUtils.getTime1());
        dbLaunch.setUserId(userId);
        dbLaunch.setUserName(userName);


        Long trashId = dbLaunch.getTrashId();
        //投放垃圾更新垃圾桶容量信息
        if (trashId != null){
            //查看垃圾桶信息根据id
            DbTrash dbTrash = dbTrashMapper.selectDbTrashById(trashId);
            //已使用容量
            BigDecimal capacity_use = dbTrash.getCapacityUse();
            //已使用重量
            BigDecimal weight_use = dbTrash.getWeightUse();

            //更新垃圾桶的已使用重量和容量
            dbTrash.setCapacityUse(capacity_use.add(dbLaunch.getCapacity()));
            dbTrash.setWeightUse(weight_use.add(dbLaunch.getWeight()));
            dbTrashMapper.updateDbTrash(dbTrash);

        }
        return dbLaunchMapper.insertDbLaunch(dbLaunch);
    }

    /**
     * 修改投放收录
     * 
     * @param dbLaunch 投放收录
     * @return 结果
     */
    @Override
    public int updateDbLaunch(DbLaunch dbLaunch)
    {
        dbLaunch.setUpdateTime(DateUtils.getNowDate());
        return dbLaunchMapper.updateDbLaunch(dbLaunch);
    }

    /**
     * 批量删除投放收录
     * 
     * @param ids 需要删除的投放收录主键
     * @return 结果
     */
    @Override
    public int deleteDbLaunchByIds(Long[] ids)
    {
        return dbLaunchMapper.deleteDbLaunchByIds(ids);
    }

    /**
     * 删除投放收录信息
     * 
     * @param id 投放收录主键
     * @return 结果
     */
    @Override
    public int deleteDbLaunchById(Long id)
    {
        return dbLaunchMapper.deleteDbLaunchById(id);
    }
}
